<template>
  <Dialog v-model="ivrValue" :title="title" width="80%" class="ivr-form-dialog">
    <div class="form-container">
      <!-- 表单头部 -->
      <div class="form-header">
        <div class="header-icon">
          <Icon icon="ep:connection" />
        </div>
        <div class="header-content">
          <h3 class="form-title">{{ title }}</h3>
          <p class="form-subtitle">创建新的IVR流程来处理语音交互</p>
        </div>
      </div>

      <!-- 表单内容 -->
      <div class="form-content">
        <el-form
          ref="formRef"
          :model="formData"
          label-width="100px"
          :rules="rules"
          class="ivr-form"
        >
          <el-form-item label="流程名称" prop="name" class="form-item">
            <el-input
              v-model="formData.name"
              placeholder="请输入流程名称"
              class="enhanced-input"
              maxlength="50"
              show-word-limit
            >
              <template #prefix>
                <Icon icon="ep:edit-pen" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="流程描述" prop="desc" class="form-item">
            <el-input
              type="textarea"
              v-model="formData.desc"
              placeholder="请输入流程描述"
              :rows="4"
              maxlength="200"
              show-word-limit
              class="enhanced-textarea"
            />
          </el-form-item>

          <el-form-item label="初始状态" prop="status" class="form-item">
            <el-radio-group v-model="formData.status" class="status-radio-group">
              <div class="radio-wrapper">
                <el-radio :label="0" class="status-radio">
                  <div class="radio-content">
                    <Icon icon="ep:edit-pen" class="radio-icon" />
                    <div class="radio-text">
                      <span class="radio-title">草稿</span>
                      <span class="radio-desc">可以继续编辑</span>
                    </div>
                  </div>
                </el-radio>
              </div>

              <div class="radio-wrapper">
                <el-radio :label="1" class="status-radio">
                  <div class="radio-content">
                    <Icon icon="ep:check" class="radio-icon" />
                    <div class="radio-text">
                      <span class="radio-title">已发布</span>
                      <span class="radio-desc">立即生效使用</span>
                    </div>
                  </div>
                </el-radio>
              </div>

              <div class="radio-wrapper">
                <el-radio :label="2" class="status-radio">
                  <div class="radio-content">
                    <Icon icon="ep:close" class="radio-icon" />
                    <div class="radio-text">
                      <span class="radio-title">已下线</span>
                      <span class="radio-desc">暂停使用</span>
                    </div>
                  </div>
                </el-radio>
              </div>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="ivrValue = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          type="primary"
          @click="submit"
          :disabled="formLoading"
          :loading="formLoading"
          class="submit-btn"
          style="margin-left: 0px !important"
        >
          <Icon icon="ep:check" class="btn-icon" />
          {{ formLoading ? '创建中...' : '确定' }}
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import { createIvrFlow } from '@/api/call/ivr/index.ts'

defineOptions({ name: 'AddIvrForm' })

const ivrValue = ref(false)
const formLoading = ref(false)
const formRef = ref(null)

const title = computed(() => {
  return '新增IVR流程'
})

const formData = reactive({
  name: '',
  desc: '',
  status: '', // 改为空字符串，确保必须选择
  groupId: ''
})

const rules = reactive({
  name: [
    { required: true, message: '流程名称不能为空', trigger: 'blur' },
    { min: 2, max: 50, message: '流程名称长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  desc: [
    { required: true, message: '流程描述不能为空', trigger: 'blur' },
    { min: 5, max: 200, message: '流程描述长度在 5 到 200 个字符', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择初始状态', trigger: 'change' },
    {
      validator: (rule, value, callback) => {
        if (value === '' || value === null || value === undefined) {
          callback(new Error('请选择初始状态'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
})

const emit = defineEmits(['success'])

const open = (val) => {
  formData.groupId = val.id
  formData.name = ''
  formData.desc = ''
  formData.status = '' // 重置为空，确保必须选择
  ivrValue.value = true

  // 重置表单验证
  nextTick(() => {
    formRef.value?.clearValidate()
  })
}

const submit = async () => {
  try {
    await formRef.value.validate()
    formLoading.value = true

    await createIvrFlow(formData)
    ElMessage.success('IVR流程创建成功')
    ivrValue.value = false
    emit('success')
  } catch (error) {
    console.error('创建IVR流程失败:', error)
    if (error !== 'validation failed') {
      ElMessage.error('创建失败，请重试')
    }
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
.ivr-form-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;
  }

  :deep(.el-dialog__header) {
    background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
    color: white;
    padding: 20px 24px;
  }

  :deep(.el-dialog__title) {
    color: white;
    font-weight: 600;
    font-size: 18px;
  }

  :deep(.el-dialog__headerbtn .el-dialog__close) {
    color: white;
    font-size: 18px;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
  }
}

.form-container {
  background: #f8fafc;
}

/* 表单头部 */
.form-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: white;
  border-bottom: 1px solid rgba(0, 82, 204, 0.1);
}

.header-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
}

.header-content {
  flex: 1;
}

.form-title {
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 4px 0;
}

.form-subtitle {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}

/* 表单内容 */
.form-content {
  padding: 24px;
}

.ivr-form {
  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
  }

  :deep(.el-form-item__error) {
    font-size: 12px;
  }
}

.form-item {
  margin-bottom: 24px;

  &:last-child {
    margin-bottom: 0;
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.enhanced-textarea {
  :deep(.el-textarea__inner) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &:focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

/* 单选框组样式 */
.status-radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.radio-wrapper {
  width: 170px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: white;
  position: relative;

  &:hover {
    border-color: #0052cc;
    background: rgba(0, 82, 204, 0.02);
  }

  &:has(.el-radio.is-checked) {
    border-color: #0052cc;
    background: rgba(0, 82, 204, 0.05);

    .radio-icon {
      color: #0052cc;
    }

    .radio-title {
      color: #0052cc;
    }
  }
}

.status-radio {
  margin: 0;
  width: 100%;

  :deep(.el-radio__input) {
    position: absolute;
    top: 16px;
    right: 16px;
  }

  :deep(.el-radio__label) {
    padding: 0;
    width: 100%;
  }
}

.radio-content {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.radio-icon {
  font-size: 20px;
  color: #6b7280;
  transition: color 0.2s ease;
}

.radio-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.radio-title {
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  transition: color 0.2s ease;
}

.radio-desc {
  font-size: 12px;
  color: #6b7280;
}

/* 底部按钮 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 20px;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid rgba(0, 82, 204, 0.1);
}

.cancel-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  border-radius: 8px;
  padding: 10px 20px;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.submit-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;

  &:hover:not(:disabled) {
    background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%);
  }

  &:disabled {
    background: #9ca3af;
    cursor: not-allowed;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 响应式设计 - H5兼容 */
@media (max-width: 768px) {
  .ivr-form-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;
    }
  }

  .form-header {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .form-content {
    padding: 16px;
  }

  .status-radio-group {
    gap: 8px;
  }

  .radio-wrapper {
    width: 170px;
    padding: 12px;
  }

  .radio-content {
    gap: 8px;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;
  }

  .cancel-btn,
  .submit-btn {
    width: 100%;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .ivr-form-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      border-radius: 0;
      height: 100vh;
    }
  }

  .form-content {
    padding: 12px;
  }

  .radio-wrapper {
    width: 170px;
    padding: 10px;
  }

  .radio-title {
    font-size: 13px;
  }

  .radio-desc {
    font-size: 11px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #1f2937;
  }

  .form-header {
    background: #374151;
    border-color: rgba(255, 255, 255, 0.1);
  }

  .form-title {
    color: #f9fafb;
  }

  .form-subtitle {
    color: #d1d5db;
  }

  .radio-wrapper {
    width: 170px;
    background: #4b5563;
    border-color: rgba(255, 255, 255, 0.1);
  }

  .radio-title {
    color: #e5e7eb;
  }

  .radio-desc {
    color: #d1d5db;
  }

  .dialog-footer {
    background: #374151;
    border-color: rgba(255, 255, 255, 0.1);
  }
}
</style>
